Belgelerim/ceng_491/implementation/doxygen/TraffEdu_Developers_Manual_Reference_ToSource/AnimationWindow.cpp

Go to the documentation of this file.
00001 
00002 osg::AnimationPath* createAnimationPath(Vehicle *p);
00003 
00004 Producer::CameraConfig *BuildConfig(void)
00005 {
00006         Producer::RenderSurface *rs1 = new Producer::RenderSurface;
00007     rs1->setScreenNum(0);
00008     rs1->setWindowName("TraffEdu-Animation");
00009     rs1->setWindowRectangle(100, 50, 750, 500 );
00010     Producer::Camera *camera1 = new Producer::Camera;
00011         Producer::Camera::Lens* lens = new Producer::Camera::Lens;
00012         lens->apply(0.0,0.0);
00013         
00014     camera1->setRenderSurface(rs1);
00015     camera1->setOffset( 0.0, 0.0 );
00016         camera1->setLens(lens);
00017 
00018 
00019     rs1->setInputRectangle( Producer::RenderSurface::InputRectangle(0.0,0.5,0.0,1.0));
00020 
00021     Producer::InputArea *ia = new Producer::InputArea;
00022     ia->addRenderSurface(rs1);
00023     Producer::CameraConfig *cfg = new Producer::CameraConfig;
00024     cfg->addCamera("Camera 1",camera1);
00025         cfg->setInputArea(ia);
00026     return cfg;
00027 }
00028 
00029 AnimationWindow::AnimationWindow(){
00030 
00031 root=new osg::Group;
00032 physTime        = 0;
00033 deltaTime       = 0;
00034 throttle        = 0;
00035 brake           = 0;
00036 steering        = 0;
00037 
00038 
00039 audio_ = new Audio;
00040 audio_->initSound();
00041 }
00042 
00043 
00044 void AnimationWindow::deselectAll(OSGWindows* osgwind)
00045 {
00046         osgwind->addCommandDeselectAll();
00047 }
00048 
00049 void AnimationWindow::processInput(double delta, osg::ref_ptr<PhysicsEventHandler> ph){
00050 
00051         if( ph->_throttleSet )
00052                 throttle += 0.25 * delta;
00053         else
00054                 throttle -= 2.5  * delta;
00055 
00056 
00057         
00058         if( ph->_brakeSet )
00059                 brake += 0.25  * delta;
00060         else if ( ph->_stop) 
00061                 brake = 1;
00062         else
00063                 brake -= 2.5 * delta;
00064 
00065 
00066         
00067         if( ph->_steerLeftSet )
00068         {
00069                 steering -= delta;
00070         }
00071         else 
00072         if( ph->_steerRightSet ) 
00073         {
00074                 steering += delta;
00075         }
00076         else 
00077                 steering -=  steering * 12 * delta; 
00078 
00079 }
00080 
00081 void AnimationWindow::setCamera(){
00082 
00083           
00084 cameraRotation.makeRotate(
00085       osg::DegreesToRadians(0.0), osg::Vec3(0,1,0), 
00086       osg::DegreesToRadians(-5.0), osg::Vec3(1,0,0) , 
00087       osg::DegreesToRadians( 0.0), osg::Vec3(0,0,1) );  
00088   
00089         cameraTrans.makeTranslate( 0,-120,20 );
00090         myCameraMatrix = cameraRotation * cameraTrans;
00091         
00092 }
00093 void AnimationWindow::setViewer(){
00094         viewer= new osgProducer::Viewer (BuildConfig());
00095         viewer->setUpViewer (osgProducer::Viewer::STANDARD_SETTINGS);
00096 }
00097 osg::ref_ptr<osgProducer::Viewer> AnimationWindow::getViewer(){
00098         return viewer;
00099 }
00100 osg::ref_ptr<osg::Group> AnimationWindow::getRoot(){
00101         return root;
00102 }
00103 
00104 void AnimationWindow::setScene(){
00105         viewer->setSceneData (root.get());
00106 }
00107 
00108 void AnimationWindow::createCarPhysics(Vehicle &vehicle,osg::ref_ptr<osg::PositionAttitudeTransform> objectPAT){
00109         CarPhysics *pCarPhys;
00110         pCarPhys = new CarPhysics(vehicle);
00111         pCarPhysVector.push_back(pCarPhys);
00112         bool success = PhysicsServer::getInstance()->registerClient(pCarPhys);
00113         CarData *carData=new CarData[1];
00114                         carData->length                 = 4.43;
00115                         carData->width                  = 1.58;
00116                         carData->height                 = 1.13;
00117                         carData->wheelRadius            = 0.35;
00118                         carData->wheelMass              = 3;
00119                         carData->chassisOffset  = 0.4;
00120                         carData->massOffset             = 0; 
00121                         carData->mass                   = 400;
00122                         carData->power                  = 450;
00123                         carData->handling               = 0; 
00124         pCarPhys->create( carData,objectPAT);
00125 
00126 }
00127 
00128 void AnimationWindow::realize(){
00129         osg::Matrixd i = myCameraMatrix.inverse(myCameraMatrix);
00130          viewer->setViewByMatrix( ( 
00131             Producer::Matrix(i.ptr() ))
00132             * Producer::Matrix::rotate( -M_PI/2.0, 1, 0, 0 ) );
00133 }
00134 void AnimationWindow::addOtherPhysicalObject(TraffEduObject* object){
00135         
00136         OtherPhysicalObjects *obj;
00137         if(object->getObjectType()==VEHICLE){
00138                 obj= new OtherPhysicalObjects(object,'p',4.43,1.58,1.13);
00139         }
00140         else{
00141                 obj =new OtherPhysicalObjects(object,'p',0.5,0.5,15);   
00142         }
00143         otherPhysicalObjects.push_back(obj);
00144         bool success = PhysicsServer::getInstance()->registerClient(obj);
00145         obj->create(object->getObjectPAT());
00146         
00147 }
00148 void AnimationWindow::generateSeq(OSGWindows* window,TrafficLight * ptr)
00149 {
00150         ptr->seqNode = new osg::Sequence;
00151         vector<Frame*> frames=window->getFrames();
00152         int j=0;
00153         int i=0;
00154         int length=(int)window->getFrames().size();
00155         
00156         for(j=0;j<length;j++)
00157         {
00158                 
00159                 TrafficLightColor color=ptr->getFrameColor(frames[j]->getFrameId());
00160                 
00161                 ptr->seqNode->addChild((TrafficModel::trafficLight(color)).get());
00162                 if(length==1) ptr->seqNode->setTime(ptr->seqNode->getNumChildren()-1, frames[j]->getFrameId());
00163                 else if(j==0) ptr->seqNode->setTime(ptr->seqNode->getNumChildren()-1, frames[j+1]->getFrameId());
00164                 else if(j==length-1) ptr->seqNode->setTime(ptr->seqNode->getNumChildren()-1, 10);
00165                 else ptr->seqNode->setTime(ptr->seqNode->getNumChildren()-1, frames[j+1]->getFrameId()- frames[j]->getFrameId());
00166                                                 
00167         }
00168                                 
00169                         
00170                 
00171                  ptr->seqNode->setMode(osg::Sequence::START);
00172   
00173                 ptr->seqNode->setInterval(osg::Sequence::LOOP, 0, -1);
00174 
00175     
00176         ptr->seqNode->setDuration(1.0f, 1);
00177         ptr->draw(root,1);
00178 }
00179 
00180 void AnimationWindow::createConstantObjects(OSGWindows *osgWindow){
00181         int i=0;
00182         int length=(int)osgWindow->getObjects().size();
00183         for(;i<length;++i){
00184                 if(osgWindow->getObjects()[i]->getCondition()=='a'){
00185                         if(osgWindow->getObjects()[i]->getObjectType()==LIGHT){ 
00186                                         TrafficLight * ptr;
00187                                         ptr=(TrafficLight * )osgWindow->getObjects()[i];
00188                                         addOtherPhysicalObject(osgWindow->getObjects()[i]);
00189                                         generateSeq(osgWindow,ptr);
00190                                         
00191                                 }
00192                         else if(osgWindow->getObjects()[i]->getObjectType()!=VEHICLE){
00193                                                                 
00194                                         addOtherPhysicalObject(osgWindow->getObjects()[i]);
00195                                         osgWindow->getObjects()[i]->draw(root);
00196                                 }
00197                 
00198                         else if(! (osgWindow->getObjects()[i]->isPhysicalVehicle())){
00199                                 Vehicle * ptr;
00200                                 ptr=(Vehicle * )osgWindow->getObjects()[i];
00201                                 createControlPoints(ptr);
00202                                 createRotationArray(ptr);
00203                                 
00204                                 ptr->setInAnimation(1);
00205                                 ptr->reloadObjectPAT();
00206                                 addOtherPhysicalObject(osgWindow->getObjects()[i]);
00207                                 osgWindow->getObjects()[i]->draw(root,1);
00208                                 double animationLength=10;
00209                                  osg::AnimationPath* animationPath = createAnimationPath(ptr);
00210                                 osgWindow->getObjects()[i]->getObjectPAT()->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1));
00211                                 
00212                         }
00213                         else{
00214                                 
00215                                 Vehicle * ptr;
00216                                 ptr=(Vehicle * )osgWindow->getObjects()[i];
00217                                 ptr->setInAnimation(1);
00218                                 ptr->reloadObjectPAT();
00219                                 ptr->reload(root);
00220                                 ptr->draw(root,1);
00221                                 createCarPhysics(* ptr,osgWindow->getObjects()[i]->getObjectPAT());
00222                                 
00223                         }
00224                 }
00225         }
00226         PhysicsServer::getInstance()->setCrashId(audio_->getSoundCrashId());
00227 
00228         if(osgWindow->getAudioFlag())
00229         alSourcePlay(audio_->getSoundId());
00230 
00231         CommandText *cmdText=new CommandText(osgWindow);
00232         root->addChild(cmdText->createTextGroup().get());
00233 
00234         
00235         
00236 }
00237 vector <CarPhysics *> AnimationWindow::getpCarPhys(){
00238         return pCarPhysVector;
00239 }
00240 
00241 vector <OtherPhysicalObjects *> AnimationWindow::getOtherPhysicalObjects(){
00242         return otherPhysicalObjects;
00243 }
00244 void AnimationWindow::odeDraw(){
00245         int length=(int)pCarPhysVector.size();
00246         for(int i=0;i<length;++i){
00247                 pCarPhysVector[i]->odeDraw();
00248         }
00249 }
00250 void AnimationWindow::exit(OSGWindows *osgWindow){
00251         audio_->killALData();
00252         int i=0;
00253         int length=(int)osgWindow->getObjects().size();
00254         for(;i<length;++i){
00255                 if(osgWindow->getObjects()[i]->getCondition()=='a'){
00256                         if(osgWindow->getObjects()[i]->getObjectType()==VEHICLE){
00257                                         Vehicle * ptr;
00258                                         ptr=(Vehicle * )osgWindow->getObjects()[i];
00259                                 if((osgWindow->getObjects()[i]->isPhysicalVehicle())){
00260                                         ptr->reload(root,1);
00261                                         ptr->undoPhysicalVehicle();
00262                                 }
00263                                 else {
00264                                         ptr->deletePath();
00265                                         ptr->resetCameraFollow();
00266                                 }
00267                                 ptr->setInAnimation(0);
00268                         }
00269                 }
00270         }
00271         PhysicsServer::getInstance()->shutdown();
00272         getViewer()->cleanup_frame();
00273 }
00274 void AnimationWindow::createControlPoints(Vehicle * ptr){
00275         int i;
00276         int length=ptr->objectPATvectorSize();
00277         osg::Vec3d position, position1, position2;
00278         
00279         
00280         for(i=0;i<length;){
00281                 if(i==0 && length>=2){
00282                         position1=ptr->getObjectPATvector()[i]->objectPAT->getPosition();
00283                         position2=ptr->getObjectPATvector()[i+1]->objectPAT->getPosition();
00284                         
00285             if(position1[0]>position2[0]){
00286                                 position[0]=position1[0]+1;
00287                         }
00288                         else{
00289                                 position[0]=position1[0]-1;
00290                         }
00291                         position[1]=position1[1];
00292                         position[2]=position1[2];
00293                         ptr->getPath()->addNewPoint (position);
00294                         ptr->getPath()->addNewPoint (position1);
00295                         ptr->getPath()->addNewPoint (position2);
00296                         
00297                         if(length==2){
00298                                 if(position1[0]>position2[0]){
00299                                         position[0]=position2[0]-1;
00300                                 }
00301                                 else{
00302                                         position[0]=position2[0]+1;
00303                                 }
00304                                 position[1]=position2[1];
00305                                 position[2]=position2[2];
00306                                 ptr->getPath()->addNewPoint (position);
00307                                 
00308                         }
00309                         i+=2;
00310                 }
00311                 else if(i==length-2){
00312                         position1=ptr->getObjectPATvector()[i]->objectPAT->getPosition();
00313                         position2=ptr->getObjectPATvector()[i+1]->objectPAT->getPosition();
00314                         
00315                         if(position1[0]>position2[0]){
00316                                 position[0]=position2[0]-1;
00317                         }
00318                         else{
00319                                 position[0]=position2[0]+1;
00320                         }
00321                         position[1]=position2[1];
00322                         position[2]=position2[2];
00323                         ptr->getPath()->addNewPoint (position1);
00324                         ptr->getPath()->addNewPoint (position2);
00325                         ptr->getPath()->addNewPoint (position);
00326                         
00327                         i+=2;
00328                 }
00329                 else if(i>0 && i==length-1){
00330                         position1=ptr->getObjectPATvector()[i-1]->objectPAT->getPosition();
00331                         position2=ptr->getObjectPATvector()[i]->objectPAT->getPosition();
00332                                 if(position1[0]>position2[0]){
00333                                         position[0]=position2[0]-1;
00334                                 }
00335                                 else{
00336                                         position[0]=position2[0]+1;
00337                                 }
00338                                 position[1]=position2[1];
00339                                 position[2]=position2[2];
00340                                 ptr->getPath()->addNewPoint (position2);
00341                                 ptr->getPath()->addNewPoint (position);
00342 
00343                         i+=1;
00344 
00345                 }
00346                 else{
00347                         position=ptr->getObjectPATvector()[i]->objectPAT->getPosition();
00348                         ptr->getPath()->addNewPoint (position);
00349                 
00350                         ++i;
00351                 }
00352 
00353         }
00354         
00355 }
00356 void AnimationWindow::followVehicle(OSGWindows *osgWindow){
00357         
00358 
00359         osg::PositionAttitudeTransform *followerPAT = new osg::PositionAttitudeTransform();
00360  
00361    followerPAT->setAttitude( osg::Quat( osg::DegreesToRadians(0.0f) , 
00362       osg::Vec3(1,0,0) ));
00363    int length=osgWindow->getObjects().size();
00364    for(int i=0;i<length;++i){
00365            if(osgWindow->getObjects()[i]->isCameraFollow()){
00366                 osgWindow->getObjects()[i]->getObjectPAT()->addChild(followerPAT);
00367                 
00368                 break;
00369            }
00370    }
00371         if (i==length) return;
00372   
00373    transformAccumulator* tankWorldCoords = new transformAccumulator();
00374    tankWorldCoords->attachToGroup(followerPAT);
00375    followNodeMatrixManipulator* followTank = new followNodeMatrixManipulator(tankWorldCoords);
00376    osgGA::KeySwitchMatrixManipulator *ksmm =getViewer()->getKeySwitchMatrixManipulator();
00377    sabitManipulator* sabit = new sabitManipulator(); 
00378    
00379    if (!ksmm)
00380       return;
00381   
00382    ksmm->addMatrixManipulator('n',"tankFollower",followTank);
00383    
00384    ksmm->addMatrixManipulator('m',"sabit",sabit);
00385    ksmm->selectMatrixManipulator(ksmm->getNumMatrixManipulators()-1);
00386 }
00387 void AnimationWindow::createRotationArray(Vehicle * ptr){
00388         ptr->getPath()->setRotationArray();
00389 }
00390 void AnimationWindow::check(Vehicle *ptr){
00391         
00392         ptr->drawPath(root);
00393         fstream outputFile( "controlPoints.txt", fstream::out );
00394         fstream out( "Points&Angles.txt", fstream::out );
00395         for(int i=0;i<3000;++i){
00396                 outputFile <<"x: "<<ptr->getPath()->getControlPointVector()[i][0] <<endl;
00397                 outputFile <<"y: "<<ptr->getPath()->getControlPointVector()[i][1] <<endl;
00398                 outputFile <<"z: "<<ptr->getPath()->getControlPointVector()[i][2] <<endl;
00399                 out <<"x: "<<ptr->getPath()->getPointVector()[i][0] <<endl;
00400                 out <<"y: "<<ptr->getPath()->getPointVector()[i][1] <<endl;
00401                 out <<"z: "<<ptr->getPath()->getPointVector()[i][2] <<endl;
00402                 out <<"angle: "<<ptr->getPath()->getRotationArray()[i] <<endl;
00403         }
00404 }
00405 osg::AnimationPath* createAnimationPath(Vehicle *ptr)
00406 {
00407    
00408     osg::AnimationPath* animationPath = new osg::AnimationPath;
00409     animationPath->setLoopMode(osg::AnimationPath::NO_LOOPING);
00410     
00411     int numSamples = 10;
00412     float roll = osg::inDegrees(30.0f);
00413         int size=ptr->getObjectPATvector().size();
00414         double looptime=10;
00415         double multiplier=1;
00416         double time_delta=0.0;
00417         double time=0.0f;
00418         int j=0;
00419 
00420         osg::Quat rotation(0.0,osg::Vec3(0,0,0));
00421         
00422         for(int i=1;i<size;i++)
00423         {
00424                 
00425                 looptime=(ptr->getObjectPATvector()[i]->frameID - ptr->getObjectPATvector()[i-1]->frameID);
00426                 
00427                 time_delta = looptime/10; 
00428                 for(;j<numSamples;++j)
00429                 {
00430                         osg::Vec3 position(ptr->getPath()->getPointVector()[j]);
00431                         rotation=osg::Quat(osg::inDegrees(ptr->getPath()->getRotationArray()[j]),osg::Vec3(0.0,0.0,1.0))*rotation;
00432                         animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation,ptr->getScale()));
00433                         time += time_delta;
00434                 }
00435                 
00436                 numSamples += 10;
00437         }
00438 
00439     return animationPath;    
00440 }
00441 
00442 
00443 void AnimationWindow::draw()
00444 {
00445 
00446         getViewer()->sync();
00447     getViewer()->update();
00448     getViewer()->frame();
00449         for(int i=0;i<getpCarPhys().size();++i){
00450                         
00451                         getpCarPhys()[i]->setSteering(steering);
00452                         getpCarPhys()[i]->setAccelleration(throttle-brake);
00453                 
00454                 }
00455         PhysicsServer::getInstance()->update(0.033);
00456         odeDraw();
00457 }
00458 void AnimationWindow::loadSkyBox(OSGWindows * osgwind){
00459         
00460         root->addChild(osgwind->getConstantRoot()->getChild(0));
00461 }

Generated on Sun May 28 18:54:08 2006 for TraffEdu by  doxygen 1.4.6-NO